package com.lantosec.hmrp.modules.system.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.system.entity.SysRole;
import com.lantosec.hmrp.modules.system.service.ISysMenuService;
import com.lantosec.hmrp.modules.system.service.ISysRoleService;
import com.lantosec.hmrp.modules.system.vo.SysRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 用户角色表 控制器
 *
 * @author AutoCode
 * @since 2020-02-14
 */
@Api(value = "用户角色表控制器", tags = "用户角色表控制器")
@RestController
@RequestMapping("/role")
@Log4j2
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService service;

    @Autowired
    private ISysMenuService menuService;

    @GetMapping("queryRoleInfo")
    @ApiOperation(value = "查询角色列表")
    public Result<List<SysRoleVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("queryAllInfoByRoleId")
    @ApiOperation(value = "根据角色信息查询权限信息及菜单信息")
    public Result<Map> queryAllInfoByRoleId(
            @ApiParam(value = "角色ID",required = true) @RequestParam(name = "roleId") String roleId
            ){
        Map map = new HashMap();
        //查询角色对应的权限信息
        List permissionList = service.queryPermissionByRuleId(roleId);

        //查询对应角色的菜单信息
        //List<TreeNodeVo> treeNodeVos = menuService.dictionariesTree("0");
        List list = service.queryMenuByRuleId(roleId);

        //查询角色对应的菜单信息
        map.put("permissionList",permissionList);
        map.put("menuInfo",list);
        return Result.ok(map);
    }

    @GetMapping("queryAllMenu")
    @ApiOperation("查询菜单树")
    public Result<List<TreeNodeVo>> getDictionariesTree() {
        return Result.ok(menuService.menuTree("0"));
    }

    @PostMapping("saveRoleInfo")
    @ApiOperation("保存角色信息")
    public Result saveRoleInfo(
            @ApiParam(value = "用户角色表VO对象", required = true) @RequestBody @Validated SysRoleVO vo, BindingResult bindingResult
    ){
        service.saveRoleInfo(vo);
        return Result.ok();
    }


    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<SysRoleVO>> page(@ApiParam(value = "分页对象", required = true) Page<SysRole> page, SysRoleVO condition){
        page = service.page(page,
                new QueryWrapper<SysRole>()
//                        .like(StringUtils.isNotEmpty(condition.getUsername()),"username",condition.getUsername())
        );
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SysRoleVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "用户角色表VO对象", required = true) @RequestBody @Validated SysRoleVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        SysRole entity = vo.convertToEntity();
        service.save(entity);
        return Result.ok(entity.getRoleId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "用户角色表VO对象", required = true) @RequestBody SysRoleVO vo){
        SysRole entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表", required = true) @PathVariable(name="ids") String ids){
        String name = service.queryRoleName(ids);
        if(StringUtils.isNotEmpty(name)){
            return Result.fail("当前角色下挂接用户：" + name + ",请先解除用户角色后再删除！");
        }else{
            service.deleteRole(ids);
        }
        return Result.ok();
    }
}
